﻿using System.IO;
using SchemeManager.Models;
using System.Windows;
using System.Collections.ObjectModel;
using SmartMeterParamFrame.Model;

using System;
using Comm.Util;
using Comm.Crypt;
using SchemeManager.SchemesServer;

namespace SchemeManager
{
    /// <summary>
    /// 方案管理
    /// </summary>
    class SchemeHelper : DependencyObject
    {
        public ObservableCollection<FACollection> Schemes { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public SchemeHelper()
        {
            this.Schemes = new ObservableCollection<FACollection>();
        }


        #region 本地方案操作
        /// <summary>
        /// 删除一个方案
        /// </summary>
        /// <param name="scheme"></param>
        /// <returns></returns>
        private bool DeleteScheme_Local(FACollection scheme)
        {
            if (scheme == null || string.IsNullOrEmpty(scheme.FAName))
                return false;
            string savePath = SchemeUtils.GetFullPath(scheme.FAName);
            if (System.IO.File.Exists(savePath))
            {
                //删除物理文件
                System.IO.File.Delete(savePath);
                //从方案结构中删除
                Schemes.Remove(scheme);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 保存方案
        /// </summary>
        /// <param name="scheme">要保存的方案</param>
        /// <returns></returns>
        private bool SaveScheme_Local(FACollection scheme)
        {
            if (scheme == null || string.IsNullOrEmpty(scheme.FAName))
                return false;
            string savePath = SchemeUtils.GetFullPath(scheme.FAName);
            try
            {
                scheme.Save(scheme.FAName, savePath, SchemeCrypt.Instance);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region 网络方案操作

        /// <summary>
        /// 加载所有的网络方案
        /// </summary>
        /// <returns></returns>
        private bool LoadAllSchemes_Net()
        {
            using (SchemesServerClient client = new SchemesServerClient())
            {
                string[] schemes = client.GetSchemes();
                foreach (string scheme in schemes)
                {
                    try
                    {
                        SchemesServer.DataInfo info = new DataInfo();
                        info.Name = scheme;
                        info.Offset = 0;
                        info.BuffLen = 1024 * 10;
                        using (System.IO.MemoryStream ms = new MemoryStream())
                        {
                            do
                            {
                                info = client.GetSchemeContentImpl(info, Comm.Util.SchemeUtils.WEB_SERVICE_PASSWORD);
                                ms.Write(info.Data, 0, info.Data.Length);
                                info.Offset = ms.Position;
                                info.Data = null;

                            } while (info.Offset != info.Length);

                            FACollection newScheme = new FACollection(ms.ToArray(), SchemeCrypt.Instance);
                            if (newScheme != null)
                            {
                                Schemes.Add(newScheme);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("加载方案:" + scheme + "失败\r\n错误原因:" + ex.Message);
                    }
                }
                client.Close();
            }
            return true;
        }

        /// <summary>
        /// 将数据保存到
        /// </summary>
        /// <param name="scheme"></param>
        /// <returns></returns>
        private bool SaveScheme_Net(FACollection scheme)
        {
            using (SchemesServerClient client = new SchemesServerClient())
            {
                byte[] content = scheme.GetBytes(Comm.Crypt.SchemeCrypt.Instance);
                using (MemoryStream ms = new MemoryStream(content))
                {
                    DataInfo info = new DataInfo();

                    info.Name = scheme.FAName;

                    info.Offset = 0;

                    info.BuffLen = 1024 * 10;

                    info.Length = ms.Length;

                    while (info.Offset != info.Length)
                    {
                        info.Data = new byte[info.Length - info.Offset <= info.BuffLen ? info.Length - info.Offset : info.BuffLen];

                        ms.Position = info.Offset;

                        ms.Read(info.Data, 0, info.Data.Length);

                        info = client.UpDataScheme(info, Comm.Util.SchemeUtils.WEB_SERVICE_PASSWORD);

                        if (info == null)
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// 从网络删除一个 方案
        /// </summary>
        /// <param name="scheme"></param>
        /// <returns></returns>
        private bool DeleteScheme_Net(FACollection scheme)
        {
            SchemesServerClient client = new SchemesServerClient();
            string ret = client.DeleteScheme(scheme.FAName, Comm.Util.SchemeUtils.WEB_SERVICE_PASSWORD);

            if (ret == string.Empty)
            {
                //从方案结构中删除
                Schemes.Remove(scheme);
                return true;
            }

            return false;
        }

        #endregion

        #region 方案操作

        /// <summary>
        /// 加载所有的方案
        /// </summary>
        /// <returns></returns>
        public bool LoadAllSchemes()
        {
            try
            {
                this.LoadAllSchemes_Net();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载方案失败:" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
        }

        /// <summary>
        /// 保存方案
        /// </summary>
        /// <param name="scheme">要保存的方案</param>
        /// <returns></returns>
        public bool SaveScheme(FACollection scheme)
        {
            try
            {
                return this.SaveScheme_Net(scheme);
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存方案失败,错误原因:" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
        }

        /// <summary>
        /// 删除一个方案
        /// </summary>
        /// <param name="scheme"></param>
        /// <returns></returns>
        public bool DeleteScheme(FACollection scheme)
        {
            try
            {
                return this.DeleteScheme_Net(scheme);
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除方案失败,错误原因:" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
        }

        #endregion
    }
}
